from flask import Flask
app = Flask(__name__)

app.config.from_pyfile("config.py")

user_list = []
blog_list = []

token = ""  # 做一个记录  标识登是否成功
import uuid


@app.route("/register/<username>/<password>", methods=["POST"])
def register(username, password):
    user_list.append({"username": username, "password": password})
    print(user_list)
    return "注册成功"


@app.route("/login/<username>/<password>", methods=["POST"])
def login(username, password):
    print(user_list)
    global token
    for item in user_list:
        if item["username"] == username and item["password"] == password:
            # 代表我们的 登录成功
            token = "123456"
            return "登录成功"

    return "用户名或密码错误"


@app.route("/addblog/<title>/<author>", methods=["POST"])
def addBlog(title, author):
    # 首先验证 令牌token是否有值 非空  令牌有值 代表 登录成功，否则代表失败
    if token:
        blog_list.append({"title": title, "author": author})
        return "添加成功"
    else:
        return "还没有登录 请登录"


@app.route("/bloglist")
def blogList():
    str = ""
    for blog in blog_list:
        str += blog["title"] + ";"
    return str


if __name__ == '__main__':
    app.run()
